#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <pwd.h>
#include <time.h>
/**
 *
 *
 *  name    file_info.c
 *  version 1.0
 *  author  Benjamin Friedrich (pib.benjamin.friedrich@htw-saarland.de)
 */

int printInodeInfo(const struct stat *inodeInfo, const char *fileName);

int main(int numberOfArguments, char **arguments)
{
	struct stat inodeInfo;

    int success = 0;
    int i       = 0;

	for(i = 1; i < numberOfArguments; i++)
	{
		if(lstat(arguments[i], &inodeInfo))
		{
			success++;
			perror(strcat("lstat -> ", arguments[i]));
		}
		else
		{
		    success += printInodeInfo(&inodeInfo, arguments[i]);
		}
	}

	return success;
}



int printInodeInfo(const struct stat *inodeInfo, const char *fileName)
{
	struct passwd *uidInfo = getpwuid(inodeInfo->st_uid);
	struct passwd *gidInfo = getpwuid(inodeInfo->st_gid);

	if(! (uidInfo && gidInfo))
	{
		perror("printInodeInfo -> getpwuid");
		return 1;
	}

	printf("\n------------------------%s-------------------------\n", fileName);
	printf("uid:  %d - %s \n", inodeInfo->st_uid, uidInfo->pw_name);
	printf("gid:  %d - %s \n", inodeInfo->st_gid, gidInfo->pw_name);
    printf("type: ");

    if(S_ISREG(inodeInfo->st_mode))
    {
    	printf("regular file\n");
    }
    else if(inodeInfo->st_mode)
    {
    	printf("directory\n");
    }
    else if(S_ISLNK(inodeInfo->st_mode))
    {
    	printf("link\n");
    }
    else
    {
    	printf("other\n");
    }

    //TODO: noch fertig machen
    printf("protection: %d\n", (inodeInfo->st_mode | S_IRWXU | S_IRWXG | S_IRWXO) );

    printf("atime: %s", ctime(&inodeInfo->st_atime));
    printf("ctime: %s", ctime(&inodeInfo->st_ctime));
    printf("mtime: %s", ctime(&inodeInfo->st_mtime));



	return 0;
}
